package com.example.demo.ieprint;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.HasInputDevices;

public class PrintTest {
    public static void main(String[] args) throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--kiosk-printing"); // 添加此选项以支持打印功能
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.example.com"); // 将URL更改为目标网站地址
        Thread.sleep(5000); // 等待5秒钟，确保页面加载完全
/*        JavascriptExecutor js = (JavascriptExecutor) driver;//模拟点击“打印”按钮
        js.executeScript("document.querySelector('button[title=打印]').click();");*/
        WebElement element=driver.findElement(By.name("q"));

        element.sendKeys(Keys.CONTROL + "p"); // 模拟Ctrl+P组合键
        element.submit();
        Thread.sleep(5000); // 等待5秒钟，确保打印对话框显示
        Alert alert = driver.switchTo().alert();
        String printDialogText = alert.getText();
        if (printDialogText != null && !printDialogText.isEmpty()) {
            System.out.println("打印对话框文本内容：" + printDialogText);
        } else {
            System.out.println("未检测到打印对话框。");
        }
        alert.accept(); // 关闭打印对话框
    }

}
